package com.swf.seed.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.swf.seed.base.SdCharset;
import com.swf.seed.base.SystemContants;

public class CmdUtils {
	private static Logger logger=LoggerFactory.getLogger(CmdUtils.class);
	
	public static String exec(String command){
		return exec(command,SdCharset.GBK);
	}
	
	public static String exec(String command,String charset){
		Process process = null;
		StringBuilder sb = new StringBuilder();
		try {
			logger.debug("执行命令:"+command);
			process = Runtime.getRuntime().exec(command);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(),charset));
            String line = null;
            while ((line = reader.readLine()) != null) {
            	sb.append(line+SystemContants.LINE_SEPARATOR);
            }
		} catch (Exception e) {
			logger.error("执行批处理指令" + command + "失败",e);
		} finally{
			process.destroy();
		}
		return sb.toString();
	}
}
